import json

from openai import OpenAI
from globle import configs as cfg

# for backward compatibility, you can still use `https://api.deepseek.com/v1` as `base_url`.
client = OpenAI(api_key=cfg.DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")

response = client.chat.completions.create(
    model="deepseek-chat",  # 必填，模型名称
    messages=[  # 必填，包装提示词的列表，有3个角色，system, user, assistant
        {"role": "system",
         "content": '你是一个判断评论是否是好评的助手，返回格式是json：{"评论":xx,"是否好评":True or False}'},
        # 系统角色，用以全局设置模型行为
        {"role": "user", "content": "这个饭店的饭好忘记"},  # 用户角色，用以设置用户输入
        # {"role": "assistant", "content": "Hi, how can I help you?"}, # 模型角色，用以设置模型输出，上下文对话时使用
    ],
    max_tokens=128,  # 返回的最大token数
    temperature=0.7,  # 温度, 取值0~2, 简单理解是温度越高随机性越强
    response_format={
        'type':'json_object'
    }
)
# print(response)
r = response.choices[0].message.content
print(type(r))
print(r)
r = json.loads(r)  # 转换成json
print(type(r))
print(r)
